package com.nf.health.app.activity.myscannerdcode;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.models.QRCodeBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.zxing.CaptureActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/7/21.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description
 * @email he_pann@163.com
 */
public class BuyHardwareActivity extends BaseActivity {

    @Bind(R.id.btn_not_scan)
    Button btnNotScan;
    @Bind(R.id.btn_scan)
    Button btnScan;
    @Bind(R.id.tv_item_name)
    TextView tvItemName;

    public static String MSG_FROM_SCAN = "MSG_FROM_SCAN";
    @Bind(R.id.tv_msg)
    WebView tvMsg;

    private String msgFromScan;
    private QRCodeBean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_buy_hardware);
        ButterKnife.bind(this);
        msgFromScan = getIntent().getStringExtra(MSG_FROM_SCAN);
        if (!TextUtils.isEmpty(msgFromScan)) {
            Gson gson = new MyGsonBuilder().createGson();
            bean = gson.fromJson(msgFromScan, QRCodeBean.class);
        }

        initView();
        initListener();
    }

    private void initView() {
        ViewUtil.setText(this, R.id.titlebar_title_tv, "领取健康管理服务");
        if (bean != null) {
            tvItemName.setText(bean.getData().getItemname());
//            tvMsg.loadData(bean.getData().getDevicemsg(), "text/html", "UTF-8");造成乱码
            tvMsg.loadDataWithBaseURL(null, bean.getData().getDevicemsg(), "text/html", "UTF-8", null);
        }
    }

    private void initListener() {
        btnNotScan.setOnClickListener(this);
        btnScan.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_scan:
                //扫码医生二维码
                Bundle bundle = new Bundle();
                bundle.putBoolean(CaptureActivity.IS_FROM_HAREWARE, true);
                bundle.putString(MyScannerPersonalDoctorActivity.DEVICE_CODE, bean.getData().getDevicecode());
                ActivityUtils.showActivity(this, CaptureActivity.class, bundle);
                finish();
                break;
            case R.id.btn_not_scan:
                Bundle bundle1 = new Bundle();
                bundle1.putString(MyScannerPersonalDoctorActivity.DEVICE_CODE, bean.getData().getDevicecode());
                bundle1.putInt(MyScannerPersonalDoctorActivity.IS_LOSE_EFFIUC, 1);
                ActivityUtils.showActivity(this, MyScannerPersonalDoctorActivity.class, bundle1);
                finish();
                break;
            default:
                break;
        }
    }
}
